import random
import requests
import pygame
import json

re = False

class cave():
    def __init__(self,log,api,screen):
        self.screen = screen
        self.log = log
        self.api = api
        self.req = requests.get("http://124.222.63.135:8000/")
        
        # self.log(self.req.text)
        self.cave = json.loads(self.req.text)
        self.screen.fill((255,255,255))
        self.api.newThread("aaa",self.t)
    def t(self):
        global re
        while not(self.pause):
            if re == True:
                re = False
                self.onStart()
    def onStart(self):
       self.screen.fill((255,255,255))
       while True:
            cave_len = random.randint(1,int(self.cave["配置"]["回声洞数量"]))
            cave = self.cave[str(cave_len)]
            if "pic" in cave["内容"] or cave["内容"] == "【数据删除】":
                pass
            else:
                break
       self.log(cave)
       if cave["内容"].__len__() >= 22:
           
        cave["内容"] = cave["内容"].replace("，","，§")

        cave["内容"] = cave["内容"].replace("。","。§")
       cave_text = ""
       for c in cave["内容"].split("§"):
           cave_text += f'"{c}"'
           cave_text += ",\n"
       
       self.log(cave_text) 
       exec(f"""self.sur = self.api.multline(
               f"回声洞 —— ({cave_len})",
               {cave_text}
               f"——{cave['投稿人']}",
               "",
               "[F1]关于  [F2]退出  [F5]刷新",
               font = pygame.font.Font(self.api.getpath("./font.ttf"),20),
               bg = (255,255,255),
               color = (0,0,0),
               center = True,
               linec = 5

        )""")
       self.screen.blit(self.sur,
               (
                   self.api.autocenter(self.sur),
                   self.api.center_verti(self.sur)))
    def pause(self):pass
    pause = False

class plugin():
    def __init__(self,log,api):
        self.log = log
        self.api = api
        req = requests.get("http://124.222.63.135:8000/")
        cave = json.loads(req.text)
        cavelen = cave["配置"]["回声洞数量"]
        self.log("""
====================================
== [yellow]回声洞[green] V 1.0[blue] (By XiaoDeng3386)[white] ==
== [red]贡献名单：XXTG666[white]              ==
====================================""")
        self.ct = pygame.surface.Surface((205,205))
        self.ct.fill((0,153,255))
        #self.ct.set_colorkey((0,0,0))
        
        self.ctfont = pygame.font.Font(self.api.getpath("./font.ttf"),25)
        self.ctfont1 = pygame.font.Font(self.api.getpath("./font.ttf"),20)
        self.ct.blit(
                self.ctfont.render("ITCraft回声洞",
                    True,
                    (255,255,255),
                    None
                ),
                (5,5)
        )
        self.ct.blit(
                self.ctfont1.render(
                    f"数据由 xxtg666 提供",
                    True,
                    (255,255,255),
                    None
                ),
                (5,75)
        )
        self.ct.blit(
                self.ctfont1.render(
                    f"总量：{cavelen}",
                    True,
                    (255,255,255),
                    None
                ),
                (5,50)
        )
        self.api.page.add("回声洞",cave,self.api.getpath("./cave/icon.png"))
        self.api.page.icon("回声洞",
                self.ct,
                color = [[0,102,255],[0,153,255]])
    def onEvent(self,event):
        global re
        if event.type == pygame.KEYUP:
            if self.api.main.getvalue("scr") == "回声洞":
                if event.key == 1073741886:
                    re = True
                    self.log(event)
                elif event.key == 1073741882:
                    self.api.dialog.msgbox("""回声洞(版本：V1.0)\n
作者：这里是小邓\n
数据提供：xxtg233""")
                elif event.key == 1073741883:
                    self.api.main.setvalue("scr","<default>")
